home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / rw / FGEMatrix.h < prev    next >
C/C++ Source or Header  |  1989-08-18  |  5KB  |  164 lines

  1. #ifndef FGEMATRIX_H
  2. #define FGEMATRIX_H
  3. #pragma once 
  4.  
  5. /*
  6.  *    Declarations for float precision general matricies.
  7.  *
  8.  *    Copyright (C) 1988, 1989.
  9.  *
  10.  *    Dr. Thomas Keffer
  11.  *    Rogue Wave Associates
  12.  *    P.O. Box 85341
  13.  *    Seattle WA 98145-1341
  14.  *
  15.  *    Permission to use, copy, modify, and distribute this
  16.  *    software and its documentation for any purpose and
  17.  *    without fee is hereby granted, provided that the
  18.  *    above copyright notice appear in all copies and that
  19.  *    both that copyright notice and this permission notice
  20.  *    appear in supporting documentation.
  21.  *    
  22.  *    This software is provided "as is" without any
  23.  *    expressed or implied warranty.
  24.  *
  25.  *
  26.  *    @(#)FGEMatrix.h    2.1    8/18/89
  27.  */
  28.  
  29. /*
  30.  *    This class is derived from class FloatVec.  Data is stored
  31.  *    FORTRAN style: by columns.
  32.  *
  33.  *    Defining the preprocessor directive "BOUNDS_CHECK" will invoke
  34.  *    bounds checking.
  35.  */
  36.  
  37. #include "FloatVec.h"
  38.  
  39. class FGEMatrix : public FloatVec {
  40.   int ncols;            // Number of columns
  41.   int nrows;            // Number of rows
  42. protected:
  43.   void             assertColRange(int);
  44.   void             assertRowRange(int);
  45.   void            assertRowCol(const FGEMatrix&);
  46.   void            assertLength(const FloatVec&);
  47.   void            assertSquare();
  48.   void            assertProduct(const FGEMatrix&);
  49.   void            assertProduct(const FloatVec&);
  50. public:
  51.   FGEMatrix();
  52.   FGEMatrix(int rows, int cols);
  53.   FGEMatrix(int rows, int cols, float initval);
  54.   FGEMatrix(const float* dat, int, int);  // Copy of dat will be made
  55.   FGEMatrix(const FloatVec& v, int, int); // Reference to v will be made
  56.   FGEMatrix(const FGEMatrix& m);       // Reference to m will be made
  57.  
  58.   float*        data()        {return FloatVec::data();}
  59.   int            cols();
  60.   int            rows();
  61.  
  62.   FGEMatrix&        reference(FGEMatrix& m); // Reference self to m
  63.   FGEMatrix        deepCopy();    // copy of self with distinct instance variables 
  64.   FGEMatrix        copy()        {return deepCopy();} // Synonym for deepCopy()
  65.   void            deepenShallowCopy();    // Guarantee that references==1:
  66.  
  67.   FloatVec        operator[](int j);    // Return a col as a slice
  68.   FloatVec        col(int j);        // Return a col as a slice
  69.   FloatVec        row(int i);        // Return a row as a slice
  70.   FloatVec        diagonal(int idiag=0);    // Return a diagonal as a slice
  71.   float&        operator()(int i, int j); // Subscripting
  72.  
  73. // Math functions
  74.   FGEMatrix        product(const FGEMatrix&); // Inner product
  75.   FloatVec        product(const FloatVec&);
  76.  
  77. // Assignment operators --- self must be same size as m
  78.   FGEMatrix&        operator=(const FGEMatrix& m);
  79.   FGEMatrix&        operator=(float);
  80.   FGEMatrix&        operator+=(const FGEMatrix& m);
  81.   FGEMatrix&        operator+=(float);
  82.   FGEMatrix&        operator-=(const FGEMatrix& m);
  83.   FGEMatrix&        operator-=(float);
  84.   FGEMatrix&        operator*=(const FGEMatrix& m);
  85.   FGEMatrix&        operator*=(float);
  86.   FGEMatrix&        operator/=(const FGEMatrix& m);
  87.   FGEMatrix&        operator/=(float);
  88.  
  89. // Increment/decrement operators
  90.   FGEMatrix&        operator++();
  91.   FGEMatrix&        operator--();
  92.  
  93. // Friendly arithmetic operators; Notice that operator* is an element-by-
  94. // element multiply, NOT a matrix multiply.
  95.   friend FGEMatrix    operator-(const FGEMatrix&);    // Unary minus
  96.   friend FGEMatrix    operator+(const FGEMatrix&);    // Unary plus
  97.   friend FGEMatrix    operator*(const FGEMatrix&, const FGEMatrix&);
  98.   friend FGEMatrix    operator/(const FGEMatrix&, const FGEMatrix&);
  99.   friend FGEMatrix    operator+(const FGEMatrix&, const FGEMatrix&);
  100.   friend FGEMatrix    operator-(const FGEMatrix&, const FGEMatrix&);
  101.   friend FGEMatrix    operator*(const FGEMatrix&, float);
  102.   friend FGEMatrix    operator*(float, const FGEMatrix&);
  103.   friend FGEMatrix    operator/(const FGEMatrix&, float);
  104.   friend FGEMatrix    operator/(float, const FGEMatrix&);
  105.   friend FGEMatrix    operator+(const FGEMatrix&, float);
  106.   friend FGEMatrix    operator+(float, const FGEMatrix&);
  107.   friend FGEMatrix    operator-(const FGEMatrix&, float);
  108.   friend FGEMatrix    operator-(float, const FGEMatrix&);
  109.  
  110. };
  111.  
  112. // Other (related) declarations:
  113. ostream&        operator<<(ostream& s, const FGEMatrix& m);
  114. FGEMatrix        transpose(const FGEMatrix&);
  115.  
  116. /******************* I N L I N E S **************************/
  117.  
  118. Inline int FGEMatrix::cols() { return ncols;}
  119. Inline int FGEMatrix::rows() { return nrows;}
  120. Inline void FGEMatrix::deepenShallowCopy(){FloatVec::deepenShallowCopy();}
  121. Inline FGEMatrix operator+(const FGEMatrix& m)        { return m; }
  122. Inline FGEMatrix operator*(float d, const FGEMatrix& m){ return m*d; }
  123. Inline FGEMatrix operator+(float d, const FGEMatrix& m){ return m+d; }
  124.  
  125. // Return a column
  126. Inline FloatVec FGEMatrix::operator[](int j){
  127. #if BOUNDS_CHECK
  128.   assertColRange(j);
  129. #endif
  130.   return FloatVec::slice(j*nrows,nrows,1);
  131. }
  132.  
  133. Inline FloatVec FGEMatrix::col(int j){    // Same as above
  134. #if BOUNDS_CHECK
  135.   assertColRange(j);
  136. #endif
  137.   return FloatVec::slice(j*nrows,nrows,1);
  138. }
  139.  
  140. Inline FloatVec FGEMatrix::row(int i){
  141. #if BOUNDS_CHECK
  142.   assertRowRange(i);
  143. #endif
  144.   return FloatVec::slice(i, ncols, nrows);
  145. }
  146.  
  147. Inline FloatVec FGEMatrix::diagonal(int i){
  148.   register int iabs=abs(i);
  149. #if BOUNDS_CHECK
  150.   assertSquare();
  151.   assertRowRange(iabs);
  152. #endif
  153.   return FloatVec::slice(i>0 ? i*nrows : iabs, nrows-iabs, nrows+1);
  154. }
  155.  
  156. Inline float& FGEMatrix::operator()(int i, int j){
  157. #if BOUNDS_CHECK
  158.   assertRowRange(i); assertColRange(j);
  159. #endif
  160.   return FloatVec::operator()(j*nrows+i);
  161. }
  162.  
  163. #endif FGEMATRIX_HXX
  164.